package com.ruoyi.webs.utils.baiduocr;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.CacheUtils;

import java.net.URLEncoder;


public class BankCard {
    public static String bankCard(String filePath) throws Exception {
        String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/bankcard";
        byte[] imgData = FileUtil.readFileByBytes(filePath);
        String imgStr = Base64Util.encode(imgData);
        String imgParam = URLEncoder.encode(imgStr, "UTF-8");

        String param = "image=" + imgParam;


        String accessToken = (String) CacheUtils.get("access_token");
        if (accessToken == null || "".equals(accessToken)) {

            accessToken = AuthService.getAuth();

            CacheUtils.put("access_token", accessToken);
        }

        String result = HttpUtil.post(url, accessToken, param);

        JSONObject resultJson = JSON.parseObject(result);
        JSONObject card_result = (JSONObject) resultJson.get("result");
        String bankCard = (String) card_result.get("bank_card_number");
        bankCard = bankCard.replaceAll(" ", "");
        return bankCard;
    }


    public static void main(String[] args) {
        String filePath = "C:/userfiles/unit/provefile/2020/3/20/bank.jpg";

        try {
            String str = bankCard(filePath);
        } catch (Exception e) {

            e.printStackTrace();
        }


        System.out.println(AuthService.getAuth());
    }
}